package com.lagou.module01.servlet;

import com.lagou.module01.bean.ClassGrade;
import com.lagou.module01.service.imp.ClassGradeServiceImp;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: xyk
 * @Date: Create in 22:52 2021/3/18
 * @Description:
 */
@WebServlet(name = "AddClassGradeServlet", urlPatterns = "/addClassGradeServlet")
public class AddClassGradeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");

        PrintWriter out = response.getWriter();

        // service层获取学员信息
        ClassGradeServiceImp ClassGradeServiceImp = new ClassGradeServiceImp();

        String classId = request.getParameter("id");
        String className = request.getParameter("className");
        String grade = request.getParameter("grade");
        String teacherName = request.getParameter("teacherName");
        String slogan = request.getParameter("slogan");
        int studentNumber = Integer.parseInt(request.getParameter("studentNumber"));

        try {
            // 编辑班级信息
            if (ClassGradeServiceImp.queryClassGradesService(classId) != null){
                ClassGradeServiceImp.editClassGradesService(new ClassGrade(classId, className, grade, teacherName, slogan, studentNumber));
            }else {
                // 添加班级
                ClassGradeServiceImp.insertClassGradesService(new ClassGrade(classId, className, grade, teacherName, slogan, studentNumber));
            }
        }
        catch (Exception e){
            out.print(e);
        }
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/classGradePageQueryServlet");
        requestDispatcher.forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
